package gui.screens;

import gui.CanvasManager;

import java.util.Vector;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 * Class for the Error screen.
 * @author Sandra Mayer
 *
 */
public class ErrorScreen extends Canvas implements CommandListener {
	
	private static ErrorScreen instance;
	private static int TOTALLENGTH = 150;
	
	private Command okCmd;
	private Vector messageVector;
	private String message;
	private Image backgroundImg;
	
	/**
	 * Private Constructor.
	 *
	 */
	private ErrorScreen() {
		okCmd = new Command("Ok", Command.EXIT, 0);
		
		try {
			backgroundImg = Image.createImage("/mainWhite.jpg");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		this.addCommand(okCmd);
		this.setCommandListener(this);
	}
	
	/**
	 * Gets the instance of the singleton.
	 * @param e the exception to show
	 * @return the instance of the error screen
	 */
	public static ErrorScreen getInstance(Exception e) {
		if (instance == null) {
			instance = new ErrorScreen();
		}
		CanvasManager.setCurScreen(instance);
		instance.message = e.toString();
		instance.convertStringToVector();
		return instance;
	}

	/**
	 * Does the command action.
	 */
	public void commandAction(Command cmd, Displayable arg1) {
		if (cmd == okCmd) {
			CanvasManager.showCurScreen();
		}
	}

	/**
	 * Paints the error screen.
	 */
	protected void paint(Graphics g) {		
		g.setColor(0xffffff);
		g.fillRect(0, 0, getWidth(), getHeight());
		g.translate((getWidth() - backgroundImg.getWidth()) / 2,
				(getHeight() - backgroundImg.getHeight()) / 2);

		g.drawImage(backgroundImg, 0, 0, Graphics.TOP | Graphics.LEFT);

		g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD,
				Font.SIZE_MEDIUM));
		g.setColor(0x000000);
		
		int x = 20;
		int y = 80;
		int length = 0;
		StringBuffer strTemp = new StringBuffer();
		for (int i = 0; i < messageVector.size(); i++) {
			String word = (String)messageVector.elementAt(i);
			if (((word.length() * 10) + length) < TOTALLENGTH) {
				strTemp.append(" " + word);
				length += (word.length() * 10) + 1;
			} else {
				length = 0;
				g.drawString(strTemp.toString(), x, y, Graphics.LEFT | Graphics.TOP);
				y += 25;
				strTemp.setLength(0);
				strTemp.append(" " + messageVector.elementAt(i));
			}	
		}
		g.drawString(strTemp.toString(), x, y, Graphics.LEFT | Graphics.TOP);
	}
	
	/**
	 * Converts the string into a vector of strings.
	 *
	 */
	private void convertStringToVector() {
		messageVector = new Vector();
		int start = 0;
		int index = message.indexOf(' ');
		while (index > 0) {
			messageVector.addElement(message.substring(start, index));
			
			start = index + 1;
			index = message.indexOf(" ", index + 1);
		}
		messageVector.addElement(message.substring(start, message.length()));
	}
}
